package com.reader.cms.controller;



import com.reader.cms.entity.sys.SysUser;
import com.reader.cms.mapper.sys.SysUserMapper;
import com.reader.cms.service.sys.ISysUserService;
import com.reader.core.entity.ResultMessageInfo;
import com.reader.core.exception.CustomException;
import com.reader.core.utils.CommonVariable;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by jiahaikukn on 2017/8/8.
 */

public abstract class BaseController<T> {

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    ISysUserService iSysUserService;

    @Autowired
    SysUserMapper sysUserMapper;

    /**
     *
     * @param code 消息CODE
     * @param message 消息message
     * @param data 返回到前端的数据对象
     * @return
     */
    protected ResultMessageInfo getSuccessResultMessage(Integer code, String message, T data){
        ResultMessageInfo successResultMessage=new ResultMessageInfo();
        successResultMessage.setCode(code);
        successResultMessage.setMsg(message);
        successResultMessage.setData(data);
        successResultMessage.setUrl(request.getRequestURI());
        return successResultMessage;

    }
    /**
     *
     * @param message 消息message
     * @param data 返回到前端的数据对象
     * @return
     */
    protected ResultMessageInfo getSuccessResultMessage(String message,T data){
        ResultMessageInfo successResultMessage=new ResultMessageInfo();
        successResultMessage.setCode(ResultMessageInfo.State.SUCCESS.getCode()); //201810125
        successResultMessage.setMsg(message);
        successResultMessage.setData(data);
        successResultMessage.setUrl(request.getRequestURI());
        return successResultMessage;

    }

    /**
     * 获取当前已登录的用户ID
     * @return
     */
/*
   protected Integer getUserId(){

       String loginID = request.getHeader(CommonVariable.LOGIN_ID);
       if(StringUtils.isBlank(loginID)){
           throw new AuthenticationException("访问凭证不合法!");
       }
       SysUser user = new SysUser();
       user.setLoginId(loginID);
       user = sysUserMapper.selectOne(user);
       return user.getId();

    }
*/


    /**
     * 获取header中的token
     * @return
     */
    protected  String getHeaderToken(){
        String token= request.getHeader(CommonVariable.TOKEN);
        if(StringUtils.isBlank(token)){
            throw new AuthenticationException("访问凭证不合法!");
        }
        return token;
    }

    /**
     * 登录成功后才可用该函数
     * @return
     */
    protected Integer getUserId(){
        String token=getHeaderToken();
        Integer userId= iSysUserService.getUserIdByToken(token);
        if(userId==null){
            throw new AuthenticationException("用户无效，请重新登录");
        }
        return userId;
    }


    /**
     * 获取LoginId
     * @return
     */
    protected String getLoginId(){
        String token=getHeaderToken();
        String loginId= iSysUserService.getLoginIdByToken(token);
        if(loginId==null){
            throw new AuthenticationException("用户无效，请重新登录");
        }
        return loginId;
    }

}
